package javolution.io;

import java.io.IOException;
import java.io.Writer;
import javolution.lang.Reusable;
import javolution.text.Text;

/* loaded from: classes2.dex */
public final class AppendableWriter extends Writer implements Reusable {
    private Appendable _output;
    private char[] _tmpBuffer;
    private final CharSequence _tmpBufferAsCharSequence = new CharSequence() { // from class: javolution.io.AppendableWriter.1
        @Override // java.lang.CharSequence
        public char charAt(int i) {
            return AppendableWriter.this._tmpBuffer[i];
        }

        @Override // java.lang.CharSequence
        public int length() {
            return AppendableWriter.this._tmpBuffer.length;
        }

        @Override // java.lang.CharSequence
        public CharSequence subSequence(int i, int i2) {
            throw new UnsupportedOperationException();
        }
    };

    @Override // java.io.Writer, java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        if (this._output != null) {
            reset();
        }
    }

    @Override // java.io.Writer, java.io.Flushable
    public void flush() {
    }

    @Override // javolution.lang.Reusable
    public void reset() {
        this._output = null;
        this._tmpBuffer = null;
    }

    public AppendableWriter setOutput(Appendable appendable) {
        if (this._output == null) {
            this._output = appendable;
            return this;
        }
        throw new IllegalStateException("Writer not closed or reset");
    }

    public void write(char c) throws IOException {
        Appendable appendable = this._output;
        if (appendable == null) {
            throw new IOException("Writer closed");
        }
        appendable.append(c);
    }

    @Override // java.io.Writer
    public void write(int i) throws IOException {
        Appendable appendable = this._output;
        if (appendable == null) {
            throw new IOException("Writer closed");
        }
        appendable.append((char) i);
    }

    public void write(CharSequence charSequence) throws IOException {
        Appendable appendable = this._output;
        if (appendable == null) {
            throw new IOException("Writer closed");
        }
        appendable.append(charSequence);
    }

    @Override // java.io.Writer
    public void write(String str, int i, int i2) throws IOException {
        Appendable appendable = this._output;
        if (appendable == null) {
            throw new IOException("Writer closed");
        }
        appendable.append(str instanceof CharSequence ? str : Text.valueOf((Object) str));
    }

    @Override // java.io.Writer
    public void write(char[] cArr, int i, int i2) throws IOException {
        Appendable appendable = this._output;
        if (appendable == null) {
            throw new IOException("Writer closed");
        }
        this._tmpBuffer = cArr;
        appendable.append(this._tmpBufferAsCharSequence, i, i2 + i);
        this._tmpBuffer = null;
    }
}
